package ray.mgocc.swing;

import ray.mgocc.title.Title;

import java.util.List;

import javax.swing.JEditorPane;
import javax.swing.JInternalFrame;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class TitleListPanel extends JInternalFrame{
    private List<Title> titles;
    private HyperlinkListener listener;
    public TitleListPanel(List<Title> titles,HyperlinkListener listener){
        super("titlelist",true,true,true,true);
        this.titles = titles;
        this.listener = listener;
        init();
    }
    public void init(){
        StringBuilder b = new StringBuilder();
        b.append("<ul>");
        for(Title title:titles){
            String name = title.getName();
            b.append("<li>");
            b.append(title.getPriority()).append(" ");
            b.append("<a href=").append(qq(name)).append(">");
            b.append(name).append(" ");
            b.append("</a>");
            b.append("[").append(title.percent()).append("%]");
            b.append("</li>");
        }
        b.append("</ul>");

        JEditorPane text = new JEditorPane();
        text.setContentType("text/html");
        text.setText(b.toString());
        text.setEditable(false);
        HyperlinkListener link = new HyperlinkListener(){
                public void hyperlinkUpdate(HyperlinkEvent e){
                    if(HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())){
                        //TODO TitlePanelを検索して置き換えさせる。
                        System.out.println(e.getDescription());
                    }
                }
            };
        text.addHyperlinkListener(listener);
        add(text);
    }
    private String qq(String str){
        return "\""+str+"\"";
    }

}

